from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *

from api.api_db import delete_drcs
from common.pd import change_dict_to_db
from api.api_view import Th_Create_Review
from api.models import DRCS
from common.pyside2_table.qt_wgt import show_msg

from api.api_project import *
from view.table_col_list import drcs_col_model_list
from view.tab_widget import Review_Table


class View_DRCS(QWidget):
    def __init__(self):
        super().__init__()
        self.view_name = "tab_drcs"
        self.data = public_data
        self.setupUi()
        self.bind()
        self.th_drcs = Th_Create_Review([])
        # self.init_cmb()

    def setupUi(self):
        # region 布局
        # 整体布局为垂直布局
        lt_pg = QVBoxLayout(self)
        fm_ctrl = QFrame()

        lt_pg.addWidget(fm_ctrl)
        lt_header = QHBoxLayout(fm_ctrl)

        # endregion

        # region 项目、制造厂及采购包选择

        # 左侧项目cmb
        left_spacer = QSpacerItem(120, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

        #     lt_header.addWidget(item)
        lt_header.addItem(left_spacer)

        # endregion

        # region 操作DRCS和IDRS
        # right_spacer = QSpacerItem(50, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)
        # lt_header.addItem(right_spacer)
        self.btn_delete = QPushButton("删除")
        self.btn_reprush = QPushButton("刷新信息")
        self.btn_create = QPushButton("发起流程")
        self.btn_save = QPushButton("保存修改")
        lt_header.addWidget(self.btn_save)
        lt_header.addWidget(self.btn_create)
        lt_header.addWidget(self.btn_reprush)
        lt_header.addWidget(self.btn_delete)
        # endregion

        # region 信息展示标签和表格
        self.lb_message = QLabel(text="已处理了XX个DRCS，待处理的DRCS数量为")
        self.lb_message.setAutoFillBackground(False)
        self.lb_message.setWordWrap(True)
        lt_pg.addWidget(self.lb_message, 0, Qt.AlignLeft)

        self.table = Review_Table("drcs", drcs_col_model_list, DRCS)
        lt_pg.addWidget(self.table)

        # endregion

    def bind(self):
        # self.cmb_project.currentTextChanged.connect(self.cmb_change)
        # self.cmb_lot_code.currentTextChanged.connect(self.cmb_change)
        # self.cmb_factory.currentTextChanged.connect(self.cmb_change)
        # self.ck_only_newest.clicked.connect(self.cmb_change)
        self.btn_create.clicked.connect(self.create_drcs)
        self.btn_reprush.clicked.connect(self.table.reset_data)
        self.btn_save.clicked.connect(lambda: self.save_drcs())
        self.btn_delete.clicked.connect(self.delete_selected)

    def update_doc_view(self, condition):
        show_msg(f"已完成查询，共查新{2}条", "blue", self.lb_message)

    def init_cmb(self):
        self.cmb_project.addItems(self.data.Project_list)
        self.cmb_factory.addItems(self.data.Factory_list)
        self.cmb_lot_code.addItems(self.data.Lot_list)

    def cmb_change(self):
        lot_code = self.cmb_lot_code.currentText()
        factory = self.cmb_factory.currentText()
        project = self.cmb_project.currentText()
        change_dic = {"lot_code": lot_code, "factory": factory, "project": project}

    def create_drcs(self):
        if self.table.change_dic:
            self.save_drcs()
        drcs_list = self.table.datas
        if drcs_list:
            new_drcs = []
            for drcs_model in drcs_list:
                if drcs_model.status == 0:
                    new_drcs.append(drcs_model)
            if new_drcs:
                self.th_drcs.model_list = new_drcs
                self.th_drcs.signal_send.connect(self.create_drcs_result)
                self.th_drcs.start()

    def save_drcs(self):
        self.table.save_change_to_db()
        self.table.change_dic = {}
        self.table.reset_data()

    def create_drcs_result(self, result=0):
        msg ="创建不成功"
        if result:
            msg = "创建完成"
        QMessageBox.information(self, "创建DRCS结果", msg, QMessageBox.Ok)


    def delete_selected(self):
        table = self.table
        items = table.selectedItems()
        row_set = set()
        for item in items:
            row_count = item.row()
            row_set.add(row_count)
        id_set = set()

        if row_set:
            for row_index in row_set:
                id = int(table.item(row_index, 0).text())
                if id and id not in row_set:
                    id_set.add(id)

            delete_drcs(list(id_set))
            return
